# -*- coding: utf-8 -*

from xlrd import *
from xlrd import xldate
from xlwt import *
from Output import Output

config = [
    # {"year": 2014, "monthstart": 5, "monthend": 12, "people": "81.xlsx", "overtime": 5},
     {"year": 2015, "monthstart": 1, "monthend": 4, "people": "20151-4.xlsx", "overtime": 5},
     {"year": 2015, "monthstart": 5, "monthend": 8, "people": "20155-8.xlsx", "overtime": 30},
     {"year": 2015, "monthstart": 9, "monthend": 12, "people": "20159-2.xlsx", "overtime": 20},
     {"year": 2016, "monthstart": 1, "monthend": 2, "people": "20159-2.xlsx", "overtime": 20},
    # {"year": 2016, "monthstart": 3, "monthend": 8, "people": "51.xlsx", "overtime": 20},
]

exclude = []

row = 0
totalW = Workbook(encoding='utf-8')
totalws = totalW.add_sheet('total')


def write_total(result):
    global row
    totalws.write(row, 0, result[0])
    totalws.write(row, 1, result[1])
    row += 1


def get_peoples(fname):
    bk = open_workbook(fname)
    sh = bk.sheet_by_index(0)
    # 获取行数
    nrows = sh.nrows
    list = []
    for i in range(0, nrows):
        list.append(sh.cell_value(i, 0))
    return list


def init_exclude():
    bk = open_workbook("exlude.xlsx")
    sh = bk.sheet_by_index(0)
    # 获取行数
    nrows = sh.nrows
    for i in range(0, nrows):
        d = xldate.xldate_as_datetime(sh.cell_value(i, 0), False)
        exclude.append(d)
    return exclude


def save(year, month, peoples, overtime):
    w = Workbook(encoding='utf-8')
    ws = w.add_sheet('考勤记录')
    output = Output(ws, peoples, year, month, exclude, overtime)
    result = output.gen()
    write_total(result)
    w.save(str(year) + "-" + str(month) + '.xls')


def main():
    init_exclude()
    for c in config:
        for i in range(c["monthstart"], c["monthend"] + 1):
            save(c["year"], i, get_peoples(c["people"]), c["overtime"])

    totalW.save('total' + '.xls')


main()
